{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb50cf98",
   "metadata": {},
   "source": [
    "# 1.人臉檢測"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "2f1d940e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "419f937f",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 1. url（目標餐廳）\n",
    "face_detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.準備參數數據\n",
    "payload = {\n",
    "    'api_key':'iFunMsDgsjGWluC2dute5E7jpaBYrlVI',\n",
    "    'api_secret':'A0tYfYO1sdLKEjUSZVDbe4Rh49lbLsmn',\n",
    "    'image_url':'https://img0.baidu.com/it/u=4122447358,1134193794&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',\n",
    "    'return_landmark':1,\n",
    "    'return_attributes':'gender,age,smiling,emotion'\n",
    "}\n",
    "\n",
    "# 3. 檢查調用的請求方法\n",
    "# 4. 請求人臉數據\n",
    "r =  requests.post(url = face_detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "3b20e9fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "603e542f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647525257,a021fa1e-2191-4b3a-b6c1-7a86deb86453',\n",
       " 'time_used': 353,\n",
       " 'faces': [{'face_token': 'fe44e451b66393b80e21eb6c5faab8ac',\n",
       "   'face_rectangle': {'top': 195, 'left': 103, 'width': 296, 'height': 296},\n",
       "   'landmark': {'contour_chin': {'x': 246, 'y': 491},\n",
       "    'contour_left1': {'x': 117, 'y': 240},\n",
       "    'contour_left2': {'x': 115, 'y': 273},\n",
       "    'contour_left3': {'x': 117, 'y': 306},\n",
       "    'contour_left4': {'x': 123, 'y': 340},\n",
       "    'contour_left5': {'x': 134, 'y': 371},\n",
       "    'contour_left6': {'x': 149, 'y': 401},\n",
       "    'contour_left7': {'x': 166, 'y': 429},\n",
       "    'contour_left8': {'x': 186, 'y': 456},\n",
       "    'contour_left9': {'x': 211, 'y': 480},\n",
       "    'contour_right1': {'x': 384, 'y': 240},\n",
       "    'contour_right2': {'x': 384, 'y': 274},\n",
       "    'contour_right3': {'x': 382, 'y': 308},\n",
       "    'contour_right4': {'x': 375, 'y': 342},\n",
       "    'contour_right5': {'x': 364, 'y': 374},\n",
       "    'contour_right6': {'x': 348, 'y': 403},\n",
       "    'contour_right7': {'x': 330, 'y': 431},\n",
       "    'contour_right8': {'x': 309, 'y': 458},\n",
       "    'contour_right9': {'x': 282, 'y': 481},\n",
       "    'left_eye_bottom': {'x': 187, 'y': 243},\n",
       "    'left_eye_center': {'x': 188, 'y': 238},\n",
       "    'left_eye_left_corner': {'x': 161, 'y': 239},\n",
       "    'left_eye_lower_left_quarter': {'x': 173, 'y': 242},\n",
       "    'left_eye_lower_right_quarter': {'x': 201, 'y': 243},\n",
       "    'left_eye_pupil': {'x': 187, 'y': 235},\n",
       "    'left_eye_right_corner': {'x': 215, 'y': 242},\n",
       "    'left_eye_top': {'x': 188, 'y': 226},\n",
       "    'left_eye_upper_left_quarter': {'x': 173, 'y': 230},\n",
       "    'left_eye_upper_right_quarter': {'x': 203, 'y': 231},\n",
       "    'left_eyebrow_left_corner': {'x': 137, 'y': 222},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 156, 'y': 219},\n",
       "    'left_eyebrow_lower_middle': {'x': 178, 'y': 218},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 201, 'y': 220},\n",
       "    'left_eyebrow_right_corner': {'x': 226, 'y': 218},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 152, 'y': 200},\n",
       "    'left_eyebrow_upper_middle': {'x': 178, 'y': 196},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 204, 'y': 201},\n",
       "    'mouth_left_corner': {'x': 180, 'y': 360},\n",
       "    'mouth_lower_lip_bottom': {'x': 246, 'y': 413},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 209, 'y': 387},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 194, 'y': 386},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 214, 'y': 405},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 286, 'y': 388},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 299, 'y': 388},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 278, 'y': 406},\n",
       "    'mouth_lower_lip_top': {'x': 247, 'y': 398},\n",
       "    'mouth_right_corner': {'x': 314, 'y': 362},\n",
       "    'mouth_upper_lip_bottom': {'x': 247, 'y': 370},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 235, 'y': 359},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 207, 'y': 357},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 213, 'y': 364},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 260, 'y': 360},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 287, 'y': 359},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 281, 'y': 365},\n",
       "    'mouth_upper_lip_top': {'x': 247, 'y': 362},\n",
       "    'nose_contour_left1': {'x': 226, 'y': 245},\n",
       "    'nose_contour_left2': {'x': 215, 'y': 299},\n",
       "    'nose_contour_left3': {'x': 223, 'y': 333},\n",
       "    'nose_contour_lower_middle': {'x': 247, 'y': 343},\n",
       "    'nose_contour_right1': {'x': 270, 'y': 245},\n",
       "    'nose_contour_right2': {'x': 282, 'y': 297},\n",
       "    'nose_contour_right3': {'x': 274, 'y': 333},\n",
       "    'nose_left': {'x': 204, 'y': 319},\n",
       "    'nose_right': {'x': 296, 'y': 318},\n",
       "    'nose_tip': {'x': 245, 'y': 322},\n",
       "    'right_eye_bottom': {'x': 313, 'y': 242},\n",
       "    'right_eye_center': {'x': 312, 'y': 236},\n",
       "    'right_eye_left_corner': {'x': 285, 'y': 240},\n",
       "    'right_eye_lower_left_quarter': {'x': 299, 'y': 241},\n",
       "    'right_eye_lower_right_quarter': {'x': 327, 'y': 240},\n",
       "    'right_eye_pupil': {'x': 312, 'y': 233},\n",
       "    'right_eye_right_corner': {'x': 339, 'y': 237},\n",
       "    'right_eye_top': {'x': 312, 'y': 225},\n",
       "    'right_eye_upper_left_quarter': {'x': 297, 'y': 229},\n",
       "    'right_eye_upper_right_quarter': {'x': 327, 'y': 229},\n",
       "    'right_eyebrow_left_corner': {'x': 277, 'y': 217},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 302, 'y': 218},\n",
       "    'right_eyebrow_lower_middle': {'x': 326, 'y': 216},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 346, 'y': 220},\n",
       "    'right_eyebrow_right_corner': {'x': 365, 'y': 229},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 300, 'y': 201},\n",
       "    'right_eyebrow_upper_middle': {'x': 327, 'y': 197},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 352, 'y': 205}},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 44},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.0,\n",
       "     'disgust': 0.0,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 100.0,\n",
       "     'neutral': 0.0,\n",
       "     'sadness': 0.0,\n",
       "     'surprise': 0.0}}}],\n",
       " 'image_id': 'FEB9zbI59C/Zmcbl3Ir7xQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "689cef62",
   "metadata": {},
   "source": [
    "# 2. 人臉分析（請嘗試調用analyize接口）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "36149a1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "516d4c03",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 1. url（目標餐廳）\n",
    "face_detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "# 2.準備參數數據\n",
    "payload = {\n",
    "    'api_key':'iFunMsDgsjGWluC2dute5E7jpaBYrlVI',\n",
    "    'api_secret':'A0tYfYO1sdLKEjUSZVDbe4Rh49lbLsmn',\n",
    "    'face_tokens':'6109819039e61cb081bef8e98ed02bb4',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality'\n",
    "}    \n",
    "\n",
    "r =  requests.post(url = face_detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "a337be2a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'attributes': {'gender': {'value': 'Male'},\n",
       "  'age': {'value': 43},\n",
       "  'facequality': {'threshold': 70.1, 'value': 89.455},\n",
       "  'headpose': {'yaw_angle': 5.3569984,\n",
       "   'pitch_angle': 5.6889143,\n",
       "   'roll_angle': -3.3102243},\n",
       "  'smile': {'threshold': 50.0, 'value': 100.0}},\n",
       " 'face_rectangle': {'width': 296, 'top': 195, 'left': 103, 'height': 296},\n",
       " 'face_token': '6109819039e61cb081bef8e98ed02bb4'}"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "id": "77821f9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "a9ce6ee4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>6109819039e61cb081bef8e98ed02bb4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>89.455</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>5.356998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>5.688914</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>-3.310224</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>296</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>195</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>296</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                 0\n",
       "face_token                        6109819039e61cb081bef8e98ed02bb4\n",
       "attributes.gender.value                                       Male\n",
       "attributes.age.value                                            43\n",
       "attributes.facequality.threshold                              70.1\n",
       "attributes.facequality.value                                89.455\n",
       "attributes.headpose.yaw_angle                             5.356998\n",
       "attributes.headpose.pitch_angle                           5.688914\n",
       "attributes.headpose.roll_angle                           -3.310224\n",
       "attributes.smile.threshold                                    50.0\n",
       "attributes.smile.value                                       100.0\n",
       "face_rectangle.width                                           296\n",
       "face_rectangle.top                                             195\n",
       "face_rectangle.left                                            103\n",
       "face_rectangle.height                                          296"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces'][0]).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a71c006",
   "metadata": {},
   "source": [
    "# 3. 人臉對比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "id": "94cae6d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "906bf1fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url（目標餐廳）\n",
    "face_detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "# 2.準備參數數據\n",
    "payload = {\n",
    "    'api_key':'iFunMsDgsjGWluC2dute5E7jpaBYrlVI',\n",
    "    'api_secret':'A0tYfYO1sdLKEjUSZVDbe4Rh49lbLsmn',\n",
    "    'image_url':'https://img0.baidu.com/it/u=2280817350,814068723&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',\n",
    "    'return_landmark':1,\n",
    "    'return_attributes':'gender,age,smiling,emotion'\n",
    "}\n",
    "\n",
    "# 3. 檢查調用的請求方法\n",
    "# 4. 請求人臉數據\n",
    "r =  requests.post(url = face_detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "c362784e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "id": "8045093c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647525271,290e3642-f241-4df3-ac8f-6452a0691423',\n",
       " 'time_used': 144,\n",
       " 'faces': [{'face_token': '1b9d3726191ad36d616ffc190e0f6112',\n",
       "   'face_rectangle': {'top': 191, 'left': 86, 'width': 290, 'height': 290},\n",
       "   'landmark': {'contour_chin': {'x': 257, 'y': 481},\n",
       "    'contour_left1': {'x': 93, 'y': 233},\n",
       "    'contour_left2': {'x': 91, 'y': 270},\n",
       "    'contour_left3': {'x': 93, 'y': 307},\n",
       "    'contour_left4': {'x': 98, 'y': 344},\n",
       "    'contour_left5': {'x': 107, 'y': 379},\n",
       "    'contour_left6': {'x': 124, 'y': 412},\n",
       "    'contour_left7': {'x': 147, 'y': 440},\n",
       "    'contour_left8': {'x': 178, 'y': 463},\n",
       "    'contour_left9': {'x': 214, 'y': 478},\n",
       "    'contour_right1': {'x': 371, 'y': 239},\n",
       "    'contour_right2': {'x': 372, 'y': 270},\n",
       "    'contour_right3': {'x': 370, 'y': 301},\n",
       "    'contour_right4': {'x': 364, 'y': 332},\n",
       "    'contour_right5': {'x': 356, 'y': 362},\n",
       "    'contour_right6': {'x': 345, 'y': 392},\n",
       "    'contour_right7': {'x': 331, 'y': 420},\n",
       "    'contour_right8': {'x': 313, 'y': 446},\n",
       "    'contour_right9': {'x': 290, 'y': 468},\n",
       "    'left_eye_bottom': {'x': 186, 'y': 243},\n",
       "    'left_eye_center': {'x': 187, 'y': 235},\n",
       "    'left_eye_left_corner': {'x': 158, 'y': 237},\n",
       "    'left_eye_lower_left_quarter': {'x': 171, 'y': 241},\n",
       "    'left_eye_lower_right_quarter': {'x': 202, 'y': 241},\n",
       "    'left_eye_pupil': {'x': 186, 'y': 232},\n",
       "    'left_eye_right_corner': {'x': 217, 'y': 239},\n",
       "    'left_eye_top': {'x': 187, 'y': 222},\n",
       "    'left_eye_upper_left_quarter': {'x': 171, 'y': 226},\n",
       "    'left_eye_upper_right_quarter': {'x': 204, 'y': 227},\n",
       "    'left_eyebrow_left_corner': {'x': 131, 'y': 215},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 153, 'y': 212},\n",
       "    'left_eyebrow_lower_middle': {'x': 177, 'y': 210},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 201, 'y': 210},\n",
       "    'left_eyebrow_right_corner': {'x': 226, 'y': 208},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 149, 'y': 195},\n",
       "    'left_eyebrow_upper_middle': {'x': 176, 'y': 190},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 203, 'y': 193},\n",
       "    'mouth_left_corner': {'x': 193, 'y': 375},\n",
       "    'mouth_lower_lip_bottom': {'x': 257, 'y': 405},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 226, 'y': 382},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 210, 'y': 390},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 231, 'y': 401},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 283, 'y': 379},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 294, 'y': 386},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 279, 'y': 398},\n",
       "    'mouth_lower_lip_top': {'x': 257, 'y': 384},\n",
       "    'mouth_right_corner': {'x': 308, 'y': 372},\n",
       "    'mouth_upper_lip_bottom': {'x': 259, 'y': 378},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 246, 'y': 366},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 220, 'y': 371},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 227, 'y': 378},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 272, 'y': 365},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 291, 'y': 368},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 284, 'y': 376},\n",
       "    'mouth_upper_lip_top': {'x': 259, 'y': 368},\n",
       "    'nose_contour_left1': {'x': 237, 'y': 240},\n",
       "    'nose_contour_left2': {'x': 229, 'y': 294},\n",
       "    'nose_contour_left3': {'x': 239, 'y': 330},\n",
       "    'nose_contour_lower_middle': {'x': 262, 'y': 334},\n",
       "    'nose_contour_right1': {'x': 276, 'y': 239},\n",
       "    'nose_contour_right2': {'x': 287, 'y': 294},\n",
       "    'nose_contour_right3': {'x': 281, 'y': 328},\n",
       "    'nose_left': {'x': 219, 'y': 320},\n",
       "    'nose_right': {'x': 297, 'y': 318},\n",
       "    'nose_tip': {'x': 264, 'y': 310},\n",
       "    'right_eye_bottom': {'x': 312, 'y': 243},\n",
       "    'right_eye_center': {'x': 311, 'y': 235},\n",
       "    'right_eye_left_corner': {'x': 286, 'y': 238},\n",
       "    'right_eye_lower_left_quarter': {'x': 298, 'y': 241},\n",
       "    'right_eye_lower_right_quarter': {'x': 325, 'y': 241},\n",
       "    'right_eye_pupil': {'x': 309, 'y': 233},\n",
       "    'right_eye_right_corner': {'x': 337, 'y': 236},\n",
       "    'right_eye_top': {'x': 311, 'y': 224},\n",
       "    'right_eye_upper_left_quarter': {'x': 296, 'y': 228},\n",
       "    'right_eye_upper_right_quarter': {'x': 325, 'y': 228},\n",
       "    'right_eyebrow_left_corner': {'x': 285, 'y': 211},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 306, 'y': 213},\n",
       "    'right_eyebrow_lower_middle': {'x': 327, 'y': 213},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 345, 'y': 215},\n",
       "    'right_eyebrow_right_corner': {'x': 363, 'y': 219},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 306, 'y': 199},\n",
       "    'right_eyebrow_upper_middle': {'x': 328, 'y': 197},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 349, 'y': 202}},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 32},\n",
       "    'smile': {'value': 99.909, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.001,\n",
       "     'disgust': 0.003,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 92.892,\n",
       "     'neutral': 7.085,\n",
       "     'sadness': 0.003,\n",
       "     'surprise': 0.016}}}],\n",
       " 'image_id': 'ERucYpGuzNeRiIHOj7Bt2Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "id": "124eb52e",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_token01 = '6109819039e61cb081bef8e98ed02bb4'\n",
    "face_token02 = '86559c1013bd9b4680e3aa2068ffcbde'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "3b7bbf49",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-143-813d7aa41439>, line 7)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-143-813d7aa41439>\"\u001b[0;36m, line \u001b[0;32m7\u001b[0m\n\u001b[0;31m    'face_token02':'86559c1013bd9b4680e3aa2068ffcbde'\u001b[0m\n\u001b[0m                  ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "payload = {\n",
    "    'api_key':'iFunMsDgsjGWluC2dute5E7jpaBYrlVI',\n",
    "    'api_secret':'A0tYfYO1sdLKEjUSZVDbe4Rh49lbLsmn',\n",
    "    'face_token01':'6109819039e61cb081bef8e98ed02bb4'\n",
    "    'face_token02':'86559c1013bd9b4680e3aa2068ffcbde'\n",
    "    \n",
    "}\n",
    "\n",
    "r =  requests.post(url = compare_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "id": "0b150ce1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 144,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "id": "6ccd64a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647525271,290e3642-f241-4df3-ac8f-6452a0691423',\n",
       " 'time_used': 144,\n",
       " 'faces': [{'face_token': '1b9d3726191ad36d616ffc190e0f6112',\n",
       "   'face_rectangle': {'top': 191, 'left': 86, 'width': 290, 'height': 290},\n",
       "   'landmark': {'contour_chin': {'x': 257, 'y': 481},\n",
       "    'contour_left1': {'x': 93, 'y': 233},\n",
       "    'contour_left2': {'x': 91, 'y': 270},\n",
       "    'contour_left3': {'x': 93, 'y': 307},\n",
       "    'contour_left4': {'x': 98, 'y': 344},\n",
       "    'contour_left5': {'x': 107, 'y': 379},\n",
       "    'contour_left6': {'x': 124, 'y': 412},\n",
       "    'contour_left7': {'x': 147, 'y': 440},\n",
       "    'contour_left8': {'x': 178, 'y': 463},\n",
       "    'contour_left9': {'x': 214, 'y': 478},\n",
       "    'contour_right1': {'x': 371, 'y': 239},\n",
       "    'contour_right2': {'x': 372, 'y': 270},\n",
       "    'contour_right3': {'x': 370, 'y': 301},\n",
       "    'contour_right4': {'x': 364, 'y': 332},\n",
       "    'contour_right5': {'x': 356, 'y': 362},\n",
       "    'contour_right6': {'x': 345, 'y': 392},\n",
       "    'contour_right7': {'x': 331, 'y': 420},\n",
       "    'contour_right8': {'x': 313, 'y': 446},\n",
       "    'contour_right9': {'x': 290, 'y': 468},\n",
       "    'left_eye_bottom': {'x': 186, 'y': 243},\n",
       "    'left_eye_center': {'x': 187, 'y': 235},\n",
       "    'left_eye_left_corner': {'x': 158, 'y': 237},\n",
       "    'left_eye_lower_left_quarter': {'x': 171, 'y': 241},\n",
       "    'left_eye_lower_right_quarter': {'x': 202, 'y': 241},\n",
       "    'left_eye_pupil': {'x': 186, 'y': 232},\n",
       "    'left_eye_right_corner': {'x': 217, 'y': 239},\n",
       "    'left_eye_top': {'x': 187, 'y': 222},\n",
       "    'left_eye_upper_left_quarter': {'x': 171, 'y': 226},\n",
       "    'left_eye_upper_right_quarter': {'x': 204, 'y': 227},\n",
       "    'left_eyebrow_left_corner': {'x': 131, 'y': 215},\n",
       "    'left_eyebrow_lower_left_quarter': {'x': 153, 'y': 212},\n",
       "    'left_eyebrow_lower_middle': {'x': 177, 'y': 210},\n",
       "    'left_eyebrow_lower_right_quarter': {'x': 201, 'y': 210},\n",
       "    'left_eyebrow_right_corner': {'x': 226, 'y': 208},\n",
       "    'left_eyebrow_upper_left_quarter': {'x': 149, 'y': 195},\n",
       "    'left_eyebrow_upper_middle': {'x': 176, 'y': 190},\n",
       "    'left_eyebrow_upper_right_quarter': {'x': 203, 'y': 193},\n",
       "    'mouth_left_corner': {'x': 193, 'y': 375},\n",
       "    'mouth_lower_lip_bottom': {'x': 257, 'y': 405},\n",
       "    'mouth_lower_lip_left_contour1': {'x': 226, 'y': 382},\n",
       "    'mouth_lower_lip_left_contour2': {'x': 210, 'y': 390},\n",
       "    'mouth_lower_lip_left_contour3': {'x': 231, 'y': 401},\n",
       "    'mouth_lower_lip_right_contour1': {'x': 283, 'y': 379},\n",
       "    'mouth_lower_lip_right_contour2': {'x': 294, 'y': 386},\n",
       "    'mouth_lower_lip_right_contour3': {'x': 279, 'y': 398},\n",
       "    'mouth_lower_lip_top': {'x': 257, 'y': 384},\n",
       "    'mouth_right_corner': {'x': 308, 'y': 372},\n",
       "    'mouth_upper_lip_bottom': {'x': 259, 'y': 378},\n",
       "    'mouth_upper_lip_left_contour1': {'x': 246, 'y': 366},\n",
       "    'mouth_upper_lip_left_contour2': {'x': 220, 'y': 371},\n",
       "    'mouth_upper_lip_left_contour3': {'x': 227, 'y': 378},\n",
       "    'mouth_upper_lip_right_contour1': {'x': 272, 'y': 365},\n",
       "    'mouth_upper_lip_right_contour2': {'x': 291, 'y': 368},\n",
       "    'mouth_upper_lip_right_contour3': {'x': 284, 'y': 376},\n",
       "    'mouth_upper_lip_top': {'x': 259, 'y': 368},\n",
       "    'nose_contour_left1': {'x': 237, 'y': 240},\n",
       "    'nose_contour_left2': {'x': 229, 'y': 294},\n",
       "    'nose_contour_left3': {'x': 239, 'y': 330},\n",
       "    'nose_contour_lower_middle': {'x': 262, 'y': 334},\n",
       "    'nose_contour_right1': {'x': 276, 'y': 239},\n",
       "    'nose_contour_right2': {'x': 287, 'y': 294},\n",
       "    'nose_contour_right3': {'x': 281, 'y': 328},\n",
       "    'nose_left': {'x': 219, 'y': 320},\n",
       "    'nose_right': {'x': 297, 'y': 318},\n",
       "    'nose_tip': {'x': 264, 'y': 310},\n",
       "    'right_eye_bottom': {'x': 312, 'y': 243},\n",
       "    'right_eye_center': {'x': 311, 'y': 235},\n",
       "    'right_eye_left_corner': {'x': 286, 'y': 238},\n",
       "    'right_eye_lower_left_quarter': {'x': 298, 'y': 241},\n",
       "    'right_eye_lower_right_quarter': {'x': 325, 'y': 241},\n",
       "    'right_eye_pupil': {'x': 309, 'y': 233},\n",
       "    'right_eye_right_corner': {'x': 337, 'y': 236},\n",
       "    'right_eye_top': {'x': 311, 'y': 224},\n",
       "    'right_eye_upper_left_quarter': {'x': 296, 'y': 228},\n",
       "    'right_eye_upper_right_quarter': {'x': 325, 'y': 228},\n",
       "    'right_eyebrow_left_corner': {'x': 285, 'y': 211},\n",
       "    'right_eyebrow_lower_left_quarter': {'x': 306, 'y': 213},\n",
       "    'right_eyebrow_lower_middle': {'x': 327, 'y': 213},\n",
       "    'right_eyebrow_lower_right_quarter': {'x': 345, 'y': 215},\n",
       "    'right_eyebrow_right_corner': {'x': 363, 'y': 219},\n",
       "    'right_eyebrow_upper_left_quarter': {'x': 306, 'y': 199},\n",
       "    'right_eyebrow_upper_middle': {'x': 328, 'y': 197},\n",
       "    'right_eyebrow_upper_right_quarter': {'x': 349, 'y': 202}},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 32},\n",
       "    'smile': {'value': 99.909, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.001,\n",
       "     'disgust': 0.003,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 92.892,\n",
       "     'neutral': 7.085,\n",
       "     'sadness': 0.003,\n",
       "     'surprise': 0.016}}}],\n",
       " 'image_id': 'ERucYpGuzNeRiIHOj7Bt2Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17764622",
   "metadata": {},
   "source": [
    "# 4. 人臉檢測函數"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "id": "0fd066d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "Api_key ='iFunMsDgsjGWluC2dute5E7jpaBYrlVI'\n",
    "Api_secret = 'A0tYfYO1sdLKEjUSZVDbe4Rh49lbLsmn'\n",
    "Image_url = 'https://img0.baidu.com/it/u=4122447358,1134193794&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "id": "21e4a64d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 思考：能否將一些功能作函數的封裝，實現更加便捷的API調用\n",
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None,calculate_all=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人臉檢測接口函數(圖片連結)\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_secret,\n",
    "    'image_url': image_url,\n",
    "    'return_landmark':landmark,\n",
    "    'return_attributes': attributes,\n",
    "    'calculate_all': calculate_all ,\n",
    "    'face_rectangle': face_rectangle,\n",
    "    'beauty_score_min':beauty_score_min,\n",
    "    'beauty_score_max':beauty_score_max   \n",
    "}\n",
    "    r =  requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "id": "92db8f47",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'API_key' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-148-26e7a84f1487>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# 調用函數\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mface_detect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mAPI_key\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mAPI_secret\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mimage_url\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'API_key' is not defined"
     ]
    }
   ],
   "source": [
    "# 調用函數\n",
    "face_detect(API_key,API_secret,image_url)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b4fea66",
   "metadata": {},
   "source": [
    "# 5 . 人脸搜索API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "id": "ccb4c5fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "f1674f96",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "face_detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'\n",
    "\n",
    "# 2.準備參數數據\n",
    "payload = {\n",
    "    'api_key':'iFunMsDgsjGWluC2dute5E7jpaBYrlVI',\n",
    "    'api_secret':'A0tYfYO1sdLKEjUSZVDbe4Rh49lbLsmn',\n",
    "    'face_token':'6109819039e61cb081bef8e98ed02bb4',\n",
    "    'faceset_token': '6d48b95a95aee5e17f30d664b4fa80e5',\n",
    "    'image_id': 'ERucYpGuzNeRiIHOj7Bt2Q=='\n",
    "}\n",
    "\n",
    "# 3. 檢查調用的請求方法\n",
    "# 4. 請求人臉數據\n",
    "r =  requests.post(url = face_detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "id": "08c15b04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "id": "4202b752",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647525297,ed207733-2dfc-43df-a821-c911a6c6614c',\n",
       " 'time_used': 294,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.389,\n",
       "   'user_id': '',\n",
       "   'face_token': '6109819039e61cb081bef8e98ed02bb4'}]}"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45cf42e7",
   "metadata": {},
   "source": [
    "# 6. 創建人臉庫（face set）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "id": "923a6b71",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "id": "c7c0d626",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "face_detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "# 2.準備參數數據\n",
    "payload = {\n",
    "    'api_key':'iFunMsDgsjGWluC2dute5E7jpaBYrlVI',\n",
    "    'api_secret':'A0tYfYO1sdLKEjUSZVDbe4Rh49lbLsmn',\n",
    "    'face_tokens':'6109819039e61cb081bef8e98ed02bb4,86559c1013bd9b4680e3aa2068ffcbde',\n",
    "    'force_merge':1 \n",
    "\n",
    "}\n",
    "\n",
    "# 3. 檢查調用的請求方法\n",
    "# 4. 請求人臉數據\n",
    "r =  requests.post(url = face_detect_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "id": "02d33b98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "id": "d02f33bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '0a0166e730a21ca8030eeced38ff4835',\n",
       " 'time_used': 668,\n",
       " 'face_count': 2,\n",
       " 'face_added': 2,\n",
       " 'request_id': '1647525302,cae8aa52-4515-4bd7-a1c5-7228b7ab1ff4',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 156,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a9a546e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2e153de",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
